app.config(function($routeProvider, $locationProvider) {
    $routeProvider.

		when('/list',{
			 templateUrl: '../home/list.html',
	         controller: 'appModuleListController'
		})
		.otherwise({
            redirectTo: '/list'
        });
});

function loginService( $http, $location ){

    return {
        checkLogin : function( recall ){
            baas_yl.call({
                'http': $http,
                'method': 'get',
                'uri': 'console/user/checkLogin',
                'success': function(d) {
                   if(d.login == 'logged'){
	                    if( typeof recall == 'function'){
	                        recall( d );
	                    }else{
	                        $location.path('/account');
	                    }
	                }else{
	                    $location.path('/login');
	                }
                }
            });
        },
        logout : function(){
            baas_yl.call({
                'http': $http,
                'method': 'get',
                'uri': 'console/user/logout',
                'success': function(d) {
                   if(d.login == 'guest'){
	                    $location.path('/login');
	                }
                }
            });
        }
    };
}
app.factory('loginService', ['$http','$location', loginService]);


app.factory('roleService', ['$http','$location', roleService]);

app.controller('appModuleListController', ['$scope', '$routeParams', '$window', '$location', '$http', 'loginService', appModuleListController]);

function appModuleListController($scope, $window, $location, $http,$routeParams, loginService){
	$scope.user = {};
    loginService.checkLogin(function( user ){
        $scope.user = user;
    });
    $scope.app_id = $routeParams.app_id;
    $scope.moduleList = [];
    $scope.getApiList = function( ){
        baas_yl.call({
                'http': $http,
                'method': 'get',
                'uri': 'console/manager/auth/list/group/',
                //'uri': 'console/user/moduleList',
                'success': function(d) {
                    if(d.status ){
		                $scope.moduleList = d.list;
		            }else{
		                $scope.description = d.description;
		            }
                }
            });
    };
    $scope.getApiList();
    
    $scope.editApiAccess = function(  ){
        baas_yl.call({
            'http': $http,
            'method': 'post',
            'uri': 'console/user/editApiAccess',
            'data' : data,
            'success': function(d) {
                if(d.status ){
	            }else{
	                $scope.description = d.description;
	            }
            }
        });
    };

    $scope.changeChecked = function(){


    };
    $scope.saveModule = function(){
        var data = {
            modules : [],
            'app_id' : $routeParams.app_id
        };
        jQuery('.modules input').each(function(){
            console.log($(this).prop("checked"));
            if( $(this).prop("checked") ){
                data.modules.push($(this).attr('id').split('-')[1]);
            }
        });
        
         baas_yl.call({
            'http': $http,
            'method': 'post',
            'uri': 'console/user/addModule',
            'data' : data,
            'success': function(d) {
            	
            }
        });
        
        //for( var i = 0; i < $scope.moduleList.length ; i ++ ){
        //    if( $scope.moduleList[i].chk ){
        //        //$scope.moduleList[i].checked = 1;
        //        //console.log($scope.moduleList[i].checked);
        //        //console.log('if');
        //        data.modules.push($scope.moduleList[i].name);
        //    }else{
        //        //$scope.moduleList[i].checked = 0;
        //    }
        //}
        //console.log($scope.moduleList);
    };
}
